package com.luck.emp.ex.web;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.emp.entity.EmpTWarehouse;
import com.luck.emp.entity.EmpTWhExD;
import com.luck.emp.entity.EmpTWhExM;
import com.luck.emp.ex.logic.IExLogic;
import com.luck.emp.ex.qry.ExQry;
import com.luck.emp.wh.logic.IWhLogic;
import com.luck.org.entity.VDepartment;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class ExAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		IExLogic exLogic;
		IWhLogic whLogic;
		/**
		 * 查询对象
		 */
		ExQry qry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		EmpTWhExM entity;
		List<EmpTWhExD> exds;
		String[] productCodes, qtys;
		List<EmpTWarehouse> whList;
		EmpTWarehouse defaultWh;// 默认仓库
		List<VDepartment> toDepts;
		/**
		 * 列表
		 */
		public String list() throws Exception{
			UserBean userInfo = super.getUserInfo();
		
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (ExQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new ExQry();
			}
			qry.setUserInfo(userInfo);
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			page = exLogic.findExPage(qry,Page.class);
			return "list";
		}
		/**
		 * 新增
		 **/
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = new EmpTWhExM();
			defaultWh =whLogic.findDefaultWarehouse(userInfo.getDeptId());
			whList = whLogic.findWarehouse(userInfo.getDeptId(), null);
			toDepts = exLogic.findExDept(userInfo);
			super.event="ADD";
			super.title="新增仓库商品调拨";
			return "edit";
		}
		/**
		 * 确认调拨出仓
		 **/
		public String confirmEx() throws Exception{
			UserBean userBean = super.getUserInfo();
			String str = "";
			try {
				str = exLogic.saveConfirmEx(userBean, qry.getExNo());;
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				super.msgStr = str;
				return super.GLOBAL_ERROR;
			}
			super.redirectUrl = "/emp/ex_list.action";
			return super.GLOBAL_SUCCESS;
		}
		/**
		 * 修改动作
		 **/
		public String edit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if("ADD".equals(super.event)){
				//新增操作
				entity.setExNo(null);
				exLogic.saveEx(userInfo,entity, productCodes, qtys);
				super.addAttribute("qry.pk", entity.getExNo());
			}else if("EDIT".equals(super.event)){
				//修改操作
				EmpTWhExM db = exLogic.get(EmpTWhExM.class,entity.getExNo());
				super.copyProperties(db, entity);
				exLogic.saveEx(userInfo,db, productCodes, qtys);
				super.addAttribute("qryHex", super.qryHex);
			}
			super.redirectUrl = "/emp/ex_list.action";
			return super.GLOBAL_SUCCESS;
		}
		/**
		 * 删除
		 **/
		public String delete() throws Exception{			
			UserBean userInfo = super.getUserInfo();
			entity = exLogic.get(EmpTWhExM.class,entity.getExNo());
			String str = "";
			try {
				str = this.exLogic.deleteEx(userInfo, entity);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				this.msgStr = str;
				super.renderJsonError(str);
			}else
				super.renderJsonSuccess("删除成功!");
			return null;	
		}
		
		/**
		 * 修改
		 **/
		public String view() throws Exception{
			entity = exLogic.get(EmpTWhExM.class,entity.getExNo());
			exds = exLogic.findExdByExCode(entity.getExNo());
			super.title="商品调拨详细信息";
			return "view";
		}
		public String print() throws Exception{
			entity = exLogic.get(EmpTWhExM.class,entity.getExNo());
			exds = exLogic.findExdByExCode(entity.getExNo());
			super.title="商品调拨详细信息";
			return "print";
		}
		
		public ExQry getQry(){
			return this.qry;
		}
		public void setQry(ExQry qry){
			this.qry = qry;
		}
		
		public IExLogic getExLogic(){
			return this.exLogic;
		}
		public void setExLogic(IExLogic exLogic){
			this.exLogic = exLogic;
		}
		public Page getPage() {
			return this.page;
		}
		public void setPage(Page page) {
			this.page = page;
		}
		public EmpTWhExM getEntity(){
			return entity;
		}
		public void setEntity(EmpTWhExM entity){
			this.entity = entity;
		}
		public List<EmpTWhExD> getExds() {
			return exds;
		}
		public void setExds(List<EmpTWhExD> exds) {
			this.exds = exds;
		}
		public String[] getProductCodes() {
			return productCodes;
		}
		public void setProductCodes(String[] productCodes) {
			this.productCodes = productCodes;
		}
		public String[] getQtys() {
			return qtys;
		}
		public void setQtys(String[] qtys) {
			this.qtys = qtys;
		}
		public IWhLogic getWhLogic() {
			return whLogic;
		}
		public void setWhLogic(IWhLogic whLogic) {
			this.whLogic = whLogic;
		}
		public List<EmpTWarehouse> getWhList() {
			return whList;
		}
		public void setWhList(List<EmpTWarehouse> whList) {
			this.whList = whList;
		}
		public EmpTWarehouse getDefaultWh() {
			return defaultWh;
		}
		public void setDefaultWh(EmpTWarehouse defaultWh) {
			this.defaultWh = defaultWh;
		}
		public List<VDepartment> getToDepts() {
			return toDepts;
		}
		public void setToDepts(List<VDepartment> toDepts) {
			this.toDepts = toDepts;
		}
		
}